include('shared.lua')

ENT.RenderGroup = RENDERGROUP_BOTH

local HealthTexture = surface.GetTextureID("redbar")

function ENT:Initialize()
	self:UpdateNW()
	
	self:UpdateHealthPos()
	
	self.Scale = 0.25
	self.BuildingCircle = 75
	self.HealthCircle = 90
	self.HealthAlpha = 125
	
	self.CreateTime = CurTime() + 0.25
end

function ENT:UpdateHealthPos()
	self.HealthBar = self:OBBCenter()
	self.HealthBar.z = 0
end

function ENT:Think()
	
end

function ENT:Draw()
	local dif = 1 - math.max( self.CreateTime - CurTime(), 0 ) / 0.25
	
	if dif < 1 then
		self:SetModelScale( Vector(1,1,1) * dif )
	end

	self:DrawModel()
end

function ENT:GetHealthTable( id, perc, InBound, OutBound )
	
	local angbegin = math.Deg2Rad( id * (360/12) )
	local angend = angbegin + math.Deg2Rad( (360/12) - 5 ) * perc
	
	return {
		{
			["x"] = math.cos( angbegin ) * OutBound,
			["y"] = math.sin( angbegin ) * OutBound,
			["u"] = 0,
			["v"] = 0
		},
		{
			["x"] = math.cos( angend ) * OutBound,
			["y"] = math.sin( angend ) * OutBound,
			["u"] = 1,
			["v"] = 1
		},
		{
			["x"] = math.cos( angend ) * InBound,
			["y"] = math.sin( angend ) * InBound,
			["u"] = 0,
			["v"] = 1
		},
		{
			["x"] = math.cos( angbegin ) * InBound,
			["y"] = math.sin( angbegin ) * InBound,
			["u"] = 1,
			["v"] = 0
		}
	}

end

function ENT:DrawBuildingBar()

	surface.SetDrawColor( 0, 255, 0, self.HealthAlpha )

	local BuildPerc = math.min( (CurTime() - self.SpawnTime) / self.BuildTime, 1.0 ) * 12
	
	for i=1, BuildPerc, 1 do
		surface.DrawPoly( self:GetHealthTable( i, 1.0, self.BuildingCircle, self.BuildingCircle + 5 ) )
	end
		
	surface.DrawPoly( 
		self:GetHealthTable( 
			math.ceil( BuildPerc ), 
			BuildPerc - math.floor( BuildPerc ), 
			self.BuildingCircle, 
			self.BuildingCircle + 5
		) 
	)

end

function ENT:DrawTeamColor()
	if self.TEAM == 1 then
		surface.SetDrawColor( 255, 255, 255, self.HealthAlpha )
	else
		surface.SetDrawColor( 255, 255, 0, self.HealthAlpha )
	end
end

function ENT:UpdateDrawAlpha()

	local EyeTrace = LocalPlayer():GetEyeTrace()
	
	if EyeTrace.Entity == self || EyeTrace.HitPos:Distance( self:GetPos() ) < self:BoundingRadius() then
		self.HealthAlpha = math.Approach( self.HealthAlpha, 255, 255 * FrameTime() )
	else
		self.HealthAlpha = math.Approach( self.HealthAlpha, 125, 255 * FrameTime() )
	end

end

function ENT:DrawHealth()

	local pos = self:LocalToWorld( self.HealthBar )
	local ang = self:LocalToWorldAngles( Angle(0,90,0) )
	local HealthPerc = self:ArcHealth() / self.MaxHealth * 12
	
	self:UpdateDrawAlpha()
	
	cam.Start3D2D( pos, ang, self.Scale )
		surface.SetTexture( HealthTexture )
		
		if self:Building() then
			self:DrawBuildingBar()			
		end
		
		self:DrawTeamColor()
		
		for i=1, HealthPerc, 1 do
			surface.DrawPoly( self:GetHealthTable( i, 1.0, self.HealthCircle, self.HealthCircle + 10 ) )
		end
		
		surface.DrawPoly( 
			self:GetHealthTable( 
				math.ceil( HealthPerc ), 
				HealthPerc - math.floor( HealthPerc ), 
				self.HealthCircle, 
				self.HealthCircle + 10
			) 
		)
		
	cam.End3D2D()

end

function ENT:DrawTranslucent()

	self:DrawHealth()
	
end

function ENT:OnRemove()
	
	local vOffset = self:GetPos()
	local Low, High = self:WorldSpaceAABB()

	local NumParticles = self:BoundingRadius()
	NumParticles = NumParticles * 4
	
	NumParticles = math.Clamp( NumParticles, 32, 256 )
		
	local emitter = ParticleEmitter( vOffset )
	
		for i=0, NumParticles do
		
			local vPos = Vector( math.Rand(Low.x,High.x), math.Rand(Low.y,High.y), math.Rand(Low.z,High.z) )
			local particle = emitter:Add( "effects/spark", vPos )
			if (particle) then
			
				particle:SetVelocity( (vPos - vOffset) * 5 )
				particle:SetLifeTime( 0 )
				particle:SetDieTime( math.Rand( 0.5, 1.0 ) )
				particle:SetStartAlpha( math.Rand( 200, 255 ) )
				particle:SetEndAlpha( 0 )
				particle:SetStartSize( 2 )
				particle:SetEndSize( 0 )
				particle:SetRoll( math.Rand(0, 360) )
				particle:SetRollDelta( 0 )
				
				particle:SetAirResistance( 100 )
				particle:SetGravity( Vector( 0, 0, -700 ) )
				particle:SetCollide( true )
				particle:SetBounce( 0.3 )
				
			end
			
		end
		
	emitter:Finish()
end